﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Maticsoft.DAL;
using System.Reflection;
using System.IO;

namespace BuildingForm.PersonInfo
{
    public partial class BankInfo : Form
    {
        Maticsoft.DAL.Ry_Info info = new Maticsoft.DAL.Ry_Info();
        Maticsoft.DAL.BankInfo bankInfo = new Maticsoft.DAL.BankInfo();
        Maticsoft.DAL.Ry_Stay_Info ry_stay_info = new Maticsoft.DAL.Ry_Stay_Info();
        private string sfzh_add = string.Empty;
        public BuildingForm.StayInfoManage f;
        public BankInfo()
        {
            InitializeComponent();
            Init();
            bind();
        }

        private MyOpaqueLayer.MyOpaqueLayer m_OpaqueLayer = null;
        private void Init()
        {
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowIcon = false;
            //表头加粗，居中
            foreach (DataGridViewColumn col in this.GridView1.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            this.GridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.GridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.GridView1.AllowUserToAddRows = false;
            this.txtName.Text = "";
            this.txtSFZH.Text = "";
            this.panel1.BorderStyle = BorderStyle.FixedSingle;
            this.panel1.Hide();

            //班组
            Maticsoft.DAL.Banz_Info info = new Banz_Info();
            DataTable dt = info.GetList("").Tables[0];
            DataRow dr3 = dt.NewRow();
            dr3["bzid"] = 0;
            dr3["bzmc"] = "全部";
            dt.Rows.InsertAt(dr3, 0);
            this.cboTeam.DisplayMember = "bzmc";
            this.cboTeam.ValueMember = "bzid";
            this.cboTeam.DataSource = dt;
             
          
 
            this.AutoScroll = true;
            panel1.Location = new Point((this.Size.Width - panel1.Size.Width) / 2, (this.Size.Height - panel1.Size.Height) / 2);
            hide();
        }
        private void show()
        {
            if (this.m_OpaqueLayer == null)
            {
                this.m_OpaqueLayer = new MyOpaqueLayer.MyOpaqueLayer(125, false);
                this.Controls.Add(this.m_OpaqueLayer);
                this.m_OpaqueLayer.Dock = DockStyle.Fill;
                this.m_OpaqueLayer.BringToFront();
            }
            this.m_OpaqueLayer.Enabled = true;
            this.m_OpaqueLayer.Visible = true;

            //panel1在悬浮层上显示

            panel1.BackColor = Color.White;

            m_OpaqueLayer.Controls.Add(panel1);
            panel1.Show();
        }
        private void hide()
        {
            panel1.Hide();
            if (this.m_OpaqueLayer != null)
            {
                this.m_OpaqueLayer.Visible = false;
                this.m_OpaqueLayer.Enabled = false;
            }
        }
        private void bind()
        {
            string name = this.txtName.Text.ToString().Trim();
            string sfzh = this.txtSFZH.Text.ToString().Trim();
            int bz =Convert.ToInt32( this.cboTeam.SelectedValue);
            string sql = "where 1=1 ";
            if (!string.IsNullOrEmpty(name))
            {
                sql += " and a.xm like '%" + name + "%'";
            }
            if (!string.IsNullOrEmpty(sfzh))
            {
                sql += " and a.sfzh like '%" + sfzh + "%'";
            }
            if (bz != 0)
            {
                sql += " and b.bzid=" + bz;
            }
            sql += " order by a.createtime desc";
            DataTable dt = info.GetListForBankInfo(sql).Tables[0];
            this.GridView1.AutoGenerateColumns = false;
            this.GridView1.DataSource = dt;
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            bind();
        }

        private void GridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            if (this.GridView1.Columns[e.ColumnIndex].Name == "colAdd")
            {
                DataGridViewRow r = GridView1.Rows[e.RowIndex];
                sfzh_add=r.Cells[2].Value.ToString();
                string xm = r.Cells[1].Value.ToString();
                string bz = r.Cells[3].Value.ToString();
                string bzid = r.Cells[7].Value.ToString();
                lblSFZH.Text = sfzh_add;
                lalName.Text = xm;
                string bankName = r.Cells[4].Value == null ? "" : r.Cells[4].Value.ToString();
                string bankCard = r.Cells[5].Value == null ? "" : r.Cells[5].Value.ToString();
                this.txtBankCard.Text = bankCard;
                this.txtBankName.Text = bankName;
                show();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string bankCard = txtBankCard.Text.ToString();
            string bankName = txtBankName.Text.ToString();
            bool isT = false;

            if (string.IsNullOrEmpty(bankName))
            {
                MessageBox.Show("银行卡不能为空！"); return;
            }
            if (string.IsNullOrEmpty(bankCard))
            {
                MessageBox.Show("卡号不能为空！"); return;
            }
            int num = bankInfo.GetList(" sfzh='" + sfzh_add+"'").Tables[0].Rows.Count;
            if (num > 0)
            {
                int id = Convert.ToInt32(bankInfo.GetList(" sfzh='" + sfzh_add+"'").Tables[0].Rows[0]["id"]);
                Maticsoft.Model.BankInfo bank = bankInfo.GetModel(id);
                bank.BankCard = bankCard;
                bank.BankName = bankName;
                isT = bankInfo.Update(bank);
            }
            else
            {
                Maticsoft.Model.BankInfo bank = new Maticsoft.Model.BankInfo();
                bank.sfzh = sfzh_add;
                bank.BankCard = txtBankCard.Text.ToString();
                bank.BankName = txtBankName.Text.ToString();
                isT = bankInfo.Add(bank) > 0;
            }
            if (isT)
            {
                MessageBox.Show("保存成功！");
            }
            else
            {
                MessageBox.Show("保存失败！");
            }
            hide();
            bind();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            hide();
        }

        private void btnQuery_Click_1(object sender, EventArgs e)
        {
            bind();
        }
 
    }
}
